var ajax = new class{

	constructor(){

		this.xmr = new XMLHttpRequest();

		this.xmr.onreadystatechange = () => {

			if(this.xmr.readyState == 4 && this.xmr.status == 200){

				let response = this.xmr.responseText;

				if(this.type == "json"){

					response = JSON.parse(response);
				}
				this.callback(response);
			}
		}
	}

	get(url, ginseng, callback, type = 'text'){

		this.initType(url, ginseng, callback, type);
		this.xmr.open("GET", this.url, true);
		this.xmr.send();
	}

	post(url, ginseng, callback, type = 'text'){

		let data = this.initType(url, ginseng, callback, type);
		this.xmr.open("POST", this.url, true);
		this.xmr.setRequestHeader('Content-Type', 'application/x-www-form-urlencodde');
		this.xmr.send(data);
	}

	initType(url, ginseng, callback, type){

		let data = this.parseParameters(ginseng);
		
		if(data.length > 0){

			this.url = url + "?" + data;

		}
		this.callback = callback;
		this.type  	  = type;
		return data;
	}

	// 字符串拼接的处理
	parseParameters(parameters){

		let strs = "";

		for(let key in parameters){

			let str = key + "=" + parameters[key];
			strs += str + "&";
		}
		return strs.substring(0, strs.length - 1);
	}
}
 